package com.fullertontech.framework.jdbc;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
import com.fullertontech.framework.global.GlobalUtils;
@Configuration
public class DataSourceConfig {
/*	@Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.driverClassName}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.pool.initialSize}")
    private Integer initialSize;
    @Value("${jdbc.pool.maxActive}")
    private Integer maxActive;*/
    
    private Integer minIdle=10;
    private Integer maxWait=10000;
    private String validationQuery="select 1 from dual";
    private boolean testOnBorrow=true;
    private boolean testOnReturn=false;
    /**
     * 空闲检测
     */
    private boolean testWhileIdle=true;
    /**
     * 空闲时间大于三分钟做一次连接检测
     */
    private long timeBetweenEvictionRunsMillis=1000*60*3;
   
    @Bean
    public PropertiesSourceAdapter PropertiesSourceAdapter() {
    	return new PropertiesSourceAdapter();
    }
    @Bean
    public DataSource dataSource(PropertiesSourceAdapter propertiesSource) throws SQLException {
    	EnableJDBC enableJDBC=GlobalUtils.getMainClass().getAnnotation(EnableJDBC.class);
    	String[] dataSourceNameArray=enableJDBC.multimediaDataSourceName();
    	Map<Object,Object> dataSourceMap=new HashMap<>();
    	DynamicDataSource dynamicDataSource=new DynamicDataSource();
    	
    	if(dataSourceNameArray.length==0) {
    		DruidDataSource druidDataSource = new DruidDataSource();
            druidDataSource.setUsername(propertiesSource.getProperties().getProperty("jdbc.username"));
            druidDataSource.setPassword(propertiesSource.getProperties().getProperty("jdbc.password"));
            druidDataSource.setDriverClassName(propertiesSource.getProperties().getProperty("jdbc.driver"));
            druidDataSource.setUrl(propertiesSource.getProperties().getProperty("jdbc.url"));
            druidDataSource.setMaxActive(Integer.parseInt(propertiesSource.getProperties().getProperty("jdbc.pool.maxActive")));
            druidDataSource.setFilters("stat");
            druidDataSource.setInitialSize(Integer.parseInt(propertiesSource.getProperties().getProperty("jdbc.pool.initialSize")));
            druidDataSource.setDbType("mysql");
            druidDataSource.setMinIdle(minIdle);
            druidDataSource.setMaxWait(maxWait);
            druidDataSource.setValidationQuery(validationQuery);
            druidDataSource.setTestOnBorrow(testOnBorrow);
            druidDataSource.setTestOnReturn(testOnReturn);
            druidDataSource.setTestWhileIdle(testWhileIdle);
            druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
            dataSourceMap.put("", druidDataSource);
            dynamicDataSource.setDefaultTargetDataSource(druidDataSource);
    	}else{
    		Stream.of(dataSourceNameArray).forEach(dataSourceName->{
    			DruidDataSource druidDataSource = new DruidDataSource();
                druidDataSource.setUsername(propertiesSource.getProperties().getProperty(dataSourceName+".jdbc.username"));
                druidDataSource.setPassword(propertiesSource.getProperties().getProperty(dataSourceName+".jdbc.password"));
                druidDataSource.setDriverClassName(propertiesSource.getProperties().getProperty(dataSourceName+".jdbc.driver"));
                druidDataSource.setUrl(propertiesSource.getProperties().getProperty(dataSourceName+".jdbc.url"));
                druidDataSource.setMaxActive(Integer.parseInt(propertiesSource.getProperties().getProperty(dataSourceName+".jdbc.pool.maxActive")));
                try {
					druidDataSource.setFilters("stat");
				} catch (SQLException e) {
					e.printStackTrace();
				}
                druidDataSource.setInitialSize(Integer.parseInt(propertiesSource.getProperties().getProperty(dataSourceName+".jdbc.pool.initialSize")));
                druidDataSource.setDbType("mysql");
                druidDataSource.setMinIdle(minIdle);
                druidDataSource.setMaxWait(maxWait);
                druidDataSource.setValidationQuery(validationQuery);
                druidDataSource.setTestOnBorrow(testOnBorrow);
                druidDataSource.setTestOnReturn(testOnReturn);
                druidDataSource.setTestWhileIdle(testWhileIdle);
                druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
                dataSourceMap.put(dataSourceName, druidDataSource);
                if(dataSourceName.equals(Stream.of(dataSourceNameArray).findFirst().get())) {
                	dynamicDataSource.setDefaultTargetDataSource(druidDataSource);
                }
    		});
    	}
    	dynamicDataSource.setTargetDataSources(dataSourceMap);
        return dynamicDataSource;
    }
}
